<?php

/**
 * 订单控制器
 */
namespace Custom\Controller;
use Think\Controller;
class OrderController extends UserController {

    function __construct()
    {
        parent::__construct();
        $this->M_order = M('Order');
        $this->M_order_detail = M('Order_detail');
        $this->M_goods = M('Goods');
        $this->M_goods_option = M('Goods_spec_option');
    }

    /**
     * 订单列表
     */
    public function index(){
        if($this->s_id){
            $where = 'b.shop_id = '.$this->s_id;
            if($_GET['user_name']){
                $where .= ' and b.user_name like\'%'.$_GET['user_name'].'%\'';
            }
            if($_GET['order_number']){
                $where .= ' and a.order_number = '.$_GET['order_number'];
            }
            if($_GET['start_time'] and $_GET['end_time']){
                $start_time = strtotime($_GET['start_time']);
                $end_time = strtotime($_GET['end_time'])+86400;
                $where .= ' and a.time > '.$start_time.' and a.time < '.$end_time;
            }
            $this->assign('get',$_GET);
            $count = $this->M_order->where($where)->join('as a left join sp_order_detail as b ON a.order_number = b.order_number')->count();
            $page_num = $this->is_page($count);
            $order_list = D('Order')->getAllOrder($page_num,$where);

            $this->assign('list', $order_list);
            $this->assign("count", $count);
        }
        $this->display();
        }

    /**
     *  支付状态修改
     */
    public function is_pay(){
        $id = $where['id'] = (int)$_GET['id'];
        $where['c_id'] = $this->c_id;
        $info = $this->M_order->find($id);
        if($info['pay_status']==1){
            $this->M_order->pay_status = 2;
            $this->M_order->pay_time = time();
            $this->M_order->where($where)->save();
            $this->ajaxReturn(array('result'=>'yes'));
        }
    }

    /**
     * 商品订单详情
     */
    public function edit(){
        $id = (int)$_GET['id'];
        $where = 'a.id='.$id;
        $info = D('Order')->getOrderDetail($where);
        if(IS_POST){
              $list['id'] = I('id');
              $data['id'] = I('oid');
              $data['order_number'] = I('order_number','','strip_tags');
              $list['price'] = I('total_price','','strip_tags');
              $list['order_number'] = I('order_number','','strip_tags');
              $data['pay_status'] = I('status');
              $list['user_name'] =  I('full_name','','strip_tags');
              $list['mobile'] = I('tel','','intval');
              $list['address'] = I('address','','strip_tags');
            if($list['order_number']&&$list['price']){
                if($_POST['pay_status']==1&&$info['pay_time']==0){
                    $data['pay_time'] = time();
                }
                $result = $this->M_order->save($data);
                $res = $this->M_order_detail->save($list);
            }
            if($result || $res)$this->success(L('adsuccess'),U('order/index'));
            else $this->error(L('failed'));
        }
            $this->assign("info",$info);
            $this->display();
        }


}